// applicationRepurchase.js
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
var md = require('../../utils/md5.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //卡劵信息
    coupons_image: '',
    coupons_name: '',
    price: '',

    coupons_bag_id: '',
    remit_way: 2,
    bank_username: '',
    bank_name: '',
    bank_card: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var coupons_bag_id = options.id
    this.setData({
      coupons_bag_id: coupons_bag_id
    })
    this.couponsInfo();
  },

  //卡劵详情
  couponsInfo: function () {
    var that = this;
    api.POST({
      params: { id: that.data.coupons_bag_id },
      url: app.globalData.coupons_bag_info_url,
      success: function (res) {
        if (res.data.code == '') {
          var coupons_info = res.data.data;
          that.setData({
            coupons_image: coupons_info.detail_image,
            coupons_name: coupons_info.name,
            price: coupons_info.buy_back_price
          })
        } else {
          if (res.data.message) {
            util.showError(res.data.message)
            wx.navigateBack({
              delta: 2
            })
          } else {
            util.showError('出错了')
          }
        }
      },
      fail: function (fail) {
        util.showError("网络错误");
      }
    })
  },

  bindUserName: function (e) {
    this.setData({
      bank_username: e.detail.value
    })
  },
  bindBankName: function (e) {
    this.setData({
      bank_name: e.detail.value
    })
  },
  bindBankCard: function (e) {
    this.setData({
      bank_card: e.detail.value
    })
  },

  // 提交申请
  submit: function (e) {
    var that = this;
    if (that.data.bank_username.length == 0) {
      util.showError('请输入开户姓名');
      return;
    }
    if (that.data.bank_name.length == 0) {
      util.showError('请输入银行名称');
      return;
    }
    if (that.data.bank_card.length == 0) {
      util.showError('请输入银行卡号');
      return;
    }
    var params = {
      coupons_bag_id: that.data.coupons_bag_id,
      remit_way: that.data.remit_way,
      bank_username: that.data.bank_username,
      bank_name: that.data.bank_name,
      bank_card: that.data.bank_card,
      price: that.data.price
    }

    api.POST({
      params: params,
      url: app.globalData.coupons_repurchase_url,
      success: function (res) {
        if (res.data.code == '') {
          wx.redirectTo({
            url: '../repurchaseSuccess/repurchaseSuccess'
          })
        } else {
          if (res.data.message == undefined) {
            util.showError('页面开小差了……')
          } else {
            util.showError(res.data.message)
          }
        }
      },
      fail: function (fail) {
        util.showError("网络错误");
      }
    })
  }
})